#ifndef GEEKOR_LOGGER_H
#define GEEKOR_LOGGER_H

#include <QThread>
#include <QMutex>
#include <QString>
#include <QStringList>

#define g_func_line QString("[%1:%2] ").arg(__FUNCTION__).arg(__LINE__)

#define logger_d(tag, msg) Logger::d(tag, g_func_line + msg)
#define logger_i(tag, msg) Logger::i(tag, g_func_line + msg)
#define logger_w(tag, msg) Logger::w(tag, g_func_line + msg)
#define logger_e(tag, msg) Logger::e(tag, g_func_line + msg)

class Logger : public QThread
{
    Q_OBJECT
public:
    struct LogData {
        QString tag;
        QString msg;
    };

    //instance
    static void enable();
    static void enableWithLogToFile();
    static void disable();
    static void release(); // invoke this when app exiting...

    static void d(const QString &tag, const QString &message);
    static void i(const QString &tag, const QString &message);
    static void w(const QString &tag, const QString &message);
    static void e(const QString &tag, const QString &message);

private:
    static Logger *mInstance;

    //////////////////////////////////////////////
public:
    explicit Logger(QObject *parent = nullptr);
    ~Logger();

    void openLog();
    void openLogToFile();
    void closeLog();

    bool isLogging() {
        return mIsRunning;
    }

signals:

public slots:

protected:
    void run();

public slots:
    void onLogComming(int type, void *data);

private:
    bool mIsRunning;
    bool mIsLogToFile;

    int m_curReadIndex;
    int m_curWriteIndex;
    QMutex m_mutexLock;
    QStringList m_messageList[2];

    void appendLog(const QString &message);
};

#endif // GEEKOR_LOGGER_H
